package view.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;

/**
 * @author wq3stone
 */

@WebFilter(urlPatterns = "/index.html")
public class IndexFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
    {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;

        HttpSession session = req.getSession();

        String uri = req.getRequestURI();

        uri = uri.substring(uri.lastIndexOf("/"));

        String path = req.getContextPath();


        if(!(session != null && session.getAttribute("user") != null))
        {
            resp.sendRedirect("/system/login.html");
            return;
//            req.getRequestDispatcher("/login.html").forward(req, res);

        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy()
    {
    }
}